package zsw.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;

import com.alibaba.fastjson.JSON;

public class TestByteIoAndStringIo {
	
	//字符流和字节流
	//字节流能处理所有的文件，而字节流只能处理字符文件
	
	public static void main(String[] args) throws Exception {
		
		//字符流
		BufferedReader reader=new BufferedReader(new FileReader(new File("E:/zsw.txt")));
		BufferedWriter writer=new BufferedWriter(new FileWriter(new File("E:/zsw1.txt") ));
		String content=null;
		while((content=reader.readLine())!=null) {
			System.out.println(content);
			writer.write(content+System.lineSeparator());
			writer.flush();
		}
		reader.close();
		writer.close();
		
		
		//字符流，带有编码的 读和写
		BufferedWriter writers=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("")),"GBK"));
        BufferedReader reader2=new BufferedReader(new InputStreamReader(new FileInputStream(new File("")), "GBK"));
        String conts=null;
        while ((conts=reader2.readLine())!=null) {
			writers.write(conts);
			writers.flush();
		}
        writers.close();
        reader2.close();
        
        
        
        
        //序列化和反序列化（字节流,不需要写入到文件中，写到字符数组流中里面）
        ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream);
        objectOutputStream.writeObject( new Object() );
        
        ByteArrayInputStream inputStream=new ByteArrayInputStream(outputStream.toByteArray());
        ObjectInputStream stream=new ObjectInputStream(inputStream);
        stream.readObject();
        
        
        //序列化和反序列化（字节流，写入到文件中）
        ObjectOutputStream objectOutputStream2=new ObjectOutputStream(new FileOutputStream(new File("")));
        objectOutputStream2.writeObject(new Object());
        
        ObjectInputStream inputStream2=new ObjectInputStream(new FileInputStream(new File("")));
        inputStream2.readObject();
        
        
        
        
        
        
        //字节流
        FileInputStream fileInputStream=new FileInputStream(new File(""));
        FileOutputStream fileOutputStream=new FileOutputStream(new File(""));
        byte[] d=new byte[1024];
        int len=0;
        while((len=fileInputStream.read(d))!=-1) {
        	fileOutputStream.write(d, 0, len);
        	fileOutputStream.flush();
        }
        fileInputStream.close();
        fileOutputStream.close();
        
        //带缓冲字节流
        //其效率比文件字节流的效率高
        BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(new File("E:/zsw.txt")));
        BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(new File("E:/zsw.txt"))) ;
        byte[] d2=new byte[1024];
        int len2=0;
        while((len2=bufferedInputStream.read(d2))!=-1) {
        	bufferedOutputStream.write(d2, 0, len2);
        	bufferedOutputStream.flush();
        }
        bufferedInputStream.close();
        bufferedOutputStream.close();
        
        
	}
	

}
